package com.greenant.store.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SaveCallback;
import com.greenant.store.R;
import com.greenant.store.bean.UserIdentification;
import com.greenant.store.utils.Const;
import com.greenant.store.utils.UtilTools;
import com.lsarah.utils.SystemBarTintManager;

public class MyAlipayBindAccountActivity extends BaseActivity {
	private EditText mEtName;
	private EditText mEtIdNum;
	private EditText mEtAlipayAccount;
	private View mViewCommit;
	
	private boolean isCommiting = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SystemBarTintManager.setContentView(this, R.layout.my_alipay_bind_account_activity,
				getResources().getColor(R.color.firebrick));
		
		mEtName = (EditText) findViewById(R.id.my_alipay_bind_account_name);
		mEtIdNum = (EditText) findViewById(R.id.my_alipay_bind_account_id_num);
		mEtAlipayAccount = (EditText) findViewById(R.id.my_alipay_bind_account_alipay_account);
		mViewCommit = findViewById(R.id.my_alipay_bind_account_commit);
		
		mViewCommit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				commit();
			}
		});
	}
	
	public void goBack(View view) {
		finish();
	}
	
	public void commit() {
		Log.d("CEXX", "CEXX-MyAlipayBindAccountActivity-commit");
		String name = mEtName.getText().toString();
		String idNum = mEtIdNum.getText().toString();
		String account = mEtAlipayAccount.getText().toString();
		
		if(TextUtils.isEmpty(name)) {
			UtilTools.showMyToast(this, getResources().getString(R.string.my_alipay_bind_account_name_hint));
			return;
		}
		
		if (TextUtils.isEmpty(idNum)) {
			UtilTools.showMyToast(this, getResources().getString(R.string.my_alipay_bind_account_id_num_hint));
			return;
		}
		
		if (TextUtils.isEmpty(account)) {
			UtilTools.showMyToast(this, getResources().getString(R.string.my_alipay_bind_account_alipay_account_hint));
			return;
		}
		
		if(AVUser.getCurrentUser() == null) {
			return;
		}
		
		if(isCommiting) {
			return;
		}
		isCommiting = true;
		final UserIdentification userIdentification = new UserIdentification();
		userIdentification.setUserId(AVUser.getCurrentUser().getObjectId());
		userIdentification.setRealName(name);
		userIdentification.setIDNum(idNum);
		userIdentification.setAccount(account);
		userIdentification.setAuthenticated(false);
		userIdentification.saveInBackground(new SaveCallback() {
			@Override
			public void done(AVException arg0) {
				isCommiting = false;
				if(arg0 == null) {
					UtilTools.showMyToast(MyAlipayBindAccountActivity.this, 
							getResources().getString(R.string.my_alipay_bind_account_bind_account_suc));
					Const.mUserIdentification = userIdentification;
					setResult(RESULT_OK);
					finish();
				} else {
					UtilTools.showMyToast(MyAlipayBindAccountActivity.this, 
							getResources().getString(R.string.my_alipay_bind_account_bind_account_fai));
				}
			}
		});
	}
}












